package com.cangxuan.backend.controller;

import com.cangxuan.backend.annotation.Check;
import com.cangxuan.backend.annotation.check.Min;
import com.cangxuan.backend.annotation.check.NotNull;
import com.cangxuan.backend.entity.User;
import com.cangxuan.backend.service.IUserGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户藏品相关接口")
@RestController
@RequestMapping("user/goods")
public class UserGoodsController extends BaseController {

    IUserGoodsService userGoodsService;
    @Autowired
    public void setUserGoodsService(IUserGoodsService userGoodsService) {
        this.userGoodsService = userGoodsService;
    }

    RedisTemplate<String, User> redisTemplate;
    @Autowired
    public void setRedisTemplate(RedisTemplate<String, User> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 我的藏品列表
     * 仅VIP会员可以访问
     * @return
     */
    @GetMapping("list")
    @PreAuthorize("hasAuthority('vip')")
//    @PreAuthorize("hasAuthority('vip') and (hasAuthority('gold') or hasAuthority('diamond'))")     //只要当前用户的权限集合只有其中一即可通过权限认证
//    @PreAuthorize("hasAnyAuthority('vip','gold','diamond')")       //相当于三个hasAuthority，使用or连接
    @ApiOperation(("【需要登录】我的藏品列表"))
    public Object list() {
        return success(userGoodsService.list());
    }

    /**
     * 购买藏品参数
     */
    @Data
    public static class UserGoodsBuyBody{
        @NotNull(message = "请传递藏品ID")
        private Integer goodsId;    //藏品ID
        @NotNull(message = "请传递购买数量")
        @Min(value = 1, message = "请传递购买1个")
        private Integer count;      //购买数量
    }

    /**
     * 购买藏品
     * 仅VIP会员可以访问
     * @return
     */
    @PostMapping("buy")
    @PreAuthorize("hasAuthority('vip')")
    @ApiOperation("【需要登录】购买藏品")
    public Object buy(@Check @RequestBody UserGoodsBuyBody userGoodsBuyBody) {
        //购买的业务逻辑，暂时空着

        return success("购买成功");
    }

    /**
     * 获取我的藏品列表
     * @return
     */
    @GetMapping("get")
    @ApiOperation("获取我的藏品列表")
    public Object get() {
        return success("请求成功", userGoodsService.list());
    }

}
